package Fenetre;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import com.Main;
import com.Clause.Clause;

public class FenetrePrincipale extends JFrame {
	private static final long serialVersionUID = 463762574027421987L;

	private JPanel panel_principal = new JPanel();
	private JLabel titre = null;
	private JButton boutonExecuter = null;

	private FilesChooserPanel fileChooserPanel = null;
	private ProgressPanel progressPanel = null;

	private PanelSeuil panelSeuil = null;
	private PanelBiais panelBiais = null;
	
	private JScrollPane scroll_clause_area = null;
	private JTextArea clauses_area = null;
	
	private JMenuBar menu = null;


	public FenetrePrincipale() {
		this.setTitle("JMALO");
		this.setLocationRelativeTo(null);
		this.setSize(800, 600);

		Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			SwingUtilities.updateComponentTreeUI(this);
			this.pack();
		} catch (Exception e) {
			e.printStackTrace();
		}

		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		panel_principal = (JPanel) this.getContentPane();

		ajouterObjetsGraphiques();

		this.pack();
		
		setLocation(
		        (screenSize.width-getWidth())/2,
		        (screenSize.height-getHeight())/2
		        );
		
		this.setVisible(true);
	}

	private void ajouterObjetsGraphiques() {
		
		ajouterMenu();

		panel_principal.setLayout(new BorderLayout());

		JPanel panel_centre = new JPanel();
		panel_centre.setLayout(new BoxLayout(panel_centre, BoxLayout.Y_AXIS));

		titre = new JLabel("",JLabel.CENTER);
		
		/*titre = new JLabel("Markov Logic Network",JLabel.CENTER);
		titre.setFont(new Font("Arial",Font.BOLD,20));*/

		panel_principal.add(titre,BorderLayout.NORTH);


		fileChooserPanel = new FilesChooserPanel();
		panel_centre.add(fileChooserPanel);

		panelBiais = new PanelBiais();
		panel_centre.add(panelBiais);
		
		clauses_area = new JTextArea();
		scroll_clause_area = new JScrollPane(clauses_area);
		scroll_clause_area.setPreferredSize(new Dimension(300,100));
		panel_centre.add(scroll_clause_area);
		scroll_clause_area.setVisible(false);

		boutonExecuter = new JButton("Executer");
		boutonExecuter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				progressPanel.setVisible(true);
				execute_main();
			}
		});
		panel_principal.add(boutonExecuter,BorderLayout.SOUTH);

		panelSeuil = new PanelSeuil();

		panel_centre.add(panelSeuil);

		progressPanel = new ProgressPanel();
		progressPanel.setVisible(false);
		panel_centre.add(progressPanel);

		panel_principal.add(panel_centre,BorderLayout.CENTER);
	}
	
	private void ajouterMenu(){
		menu = new JMenuBar();
		JMenu menu_BD = new JMenu("Base de données");
		menu.add(menu_BD);
		JMenuItem BD_configurer = new JMenuItem("Configurer");
		BD_configurer.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				@SuppressWarnings("unused")
				BDConfiguratorDialog bd_config = new BDConfiguratorDialog();
			}
		});
		menu_BD.add(BD_configurer);
		this.setJMenuBar(menu);
	}

	public void setProgressValue(int etape,int value) {
		progressPanel.setProgressValue(etape,value);
	}

	public void setTemps(float temps){
		int temps_min = (int) (temps/60);
		titre.setText(new Float(temps).toString()+ " secondes | "+ temps_min +" min");
		this.pack();
	}
	
	public void afficherClauses(ArrayList<Clause> clauses) {
		
		StringBuffer sb = new StringBuffer();
		
		for(Clause clause : clauses){
		//	Biais.enleverImplique(clause);
		//	Biais.enleverDoubleNegations(clause);
			sb.append(clause).append("\n");
		}
		
		clauses_area.setText(sb.toString());
		
		progressPanel.setVisible(false);
		progressPanel.vider();
		scroll_clause_area.setVisible(true);
		
		this.pack();
		
	}
	
	public void execute_main() {
		final FenetrePrincipale fenetre = this;
		final File fichier_info_t = fileChooserPanel.getFichierInfo();
		final File fichier_type_t = fileChooserPanel.getFichierType();
		final File[] fichiers_db_t = fileChooserPanel.getFichiersDb();

		if(fichiers_db_t != null){

			final int seuil = panelSeuil.getSeuil();
			System.out.println(seuil);
			panelBiais.executer();
			//fileChooserPanel.setVisible(true);
			progressPanel.setVisible(true);
			this.pack();
			Thread th = new Thread(new Runnable() {
				public void run() {
					Main main = new Main();
					float temps = main.executer(fenetre, fichier_info_t, fichier_type_t,
							fichiers_db_t,seuil);
					fenetre.setTemps(temps);
				}
			});
			th.start();
		}
	}

	@SuppressWarnings("unused")
	public static void main(String[] args) {
		FenetrePrincipale fenetre = new FenetrePrincipale();
	}

}
